<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
session_start();

class welcome extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('Dao');
        $this->load->library('email');
    }

    public function index() {
        $data['title'] = 'Welcome to wfaces.com';
        $data['description'] = 'NONE';
        $data['keywords'] = 'Key, words, of, the, page';
        $data['view'] = 'v_signup';
        $data['left_view'] = 'v_find';
        $data['h_view'] = 'header';

        $this->load->view('templates/welcomeTemplate', $data);
    }

    public function signup() {
        $data['title'] = 'Welcome to wfaces.com';
        $data['description'] = 'NONE';
        $data['keywords'] = 'Key, words, of, the, page';
        $data['view'] = 'v_signup';
        $data['left_view'] = 'v_find';
        $data['h_view'] = 'header';
        if (!$this->input->post('conditions')) {
            $data['conditions'] = 'error';
            $this->load->view('templates/welcomeTemplate', $data);
            return;
        }

        $birth = $this->input->post('birthday');
        if ($birth != null && $birth != "") {
            $dt = explode("/", $birth);
            $d = $dt[0];
            $m = $dt[1];
            $y = $dt[2];
            $user['birthday'] = date('Y-m-d', strtotime($y . "-" . $m . "-" . $d));
        }

        //echo $this->Dao->getAgeFromBirthday($user['birthday']);
        if ($this->Dao->getAgeFromBirthday($user['birthday']) < 13) {
            $data['age'] = 'error';
            $this->load->view('templates/welcomeTemplate', $data);
            return;
        }

        $user['firstname'] = $this->input->post('firstname');
        $user['lastname'] = $this->input->post('lastname');
        $user['password'] = $this->input->post('password');
        $user['mail'] = $this->input->post('email');
        $user['login'] = $this->input->post('email');
        $user['statut'] = "a-online";

        $user['sexe'] = $this->input->post('sexe');
        $ret = $this->Dao->insertUser($user);

        if ($ret == 1) {
            if ($this->Dao->verifOnDb($user['mail'], $user['password'])) {
                redirect('profile/', 'refresh');
            }
        }

    }

    public function mail() {
        echo "envoi du message en cours ...";
        $this->email->from('wfaces.info@gmail.com', 'Lhoussin GHANEM');
        $this->email->to('ghanem.lhoussin@gmail.com');
        $this->email->cc('');
        $this->email->bcc('');

        $this->email->subject('Email Test');
        $this->email->message('Testing the email sending on wfaces.');
        $this->email->set_newline("\r\n");
        $this->email->send();

        echo $this->email->print_debugger();


    }
}

?>
